<?php

class Logger {

	private $tableName;
	private $path;
	
	private static $thisLogger = null;
	
	private function __construct($tableName) {
		//*/
		$this->tableName = $tableName;
		$parts = array( 
			"log", 
			"log/".date("Y"), 
			"log/".date("Y/m") 
		);
		foreach( $parts as $part ){
			if( is_dir($part) !== TRUE ){
				mkdir($part);
				chmod($part, 0777);
			}
			$this->path = $part;
		}
		return $this;
		/**/
	}
	
	public static function createInstance( $tableName ){
		if( self::$thisLogger == null )
			self::$thisLogger = new Logger($tableName);
		return self::$thisLogger;
	}
	
	public function log($operation, $sql) {
		//*/
		//if( isset($_SESSION['userData']['nome']) ) echo $_SESSION['userData']['nome']." - AKII<br />";
		$dt = explode(" ", date("Y-m-d H:i:s", strtotime("-3 hours")));
		$line = 	$dt[0] 									// Data
					."\t".$dt[1]							// Hora
					."\t".$_SESSION['userData']['nome'] 	// Usuário
					."\t".$operation						// Operação no banco
					."\t".$this->tableName					// Tabela afetada pela query
					."\t\"".$sql."\"\n";					// Query
		// "a" representa que o arquivo é aberto para ser escrito
		$fp = fopen($this->path."/".date("d").".log", "a");
		// Escreve "$line" no ${date('d')}.log
		$escreve = fwrite($fp, $line);
		// Fecha o arquivo
		fclose($fp);
		/**/
	}
	
}

?>